﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;
using EIA.SLClient.PlatManage.Infrastructure;

namespace EIA.SLClient.PlatManage.PMMgr
{
    [Export("EIA.SLClient.PlatManage.PMMgr.PMMgrNavigationView")]
    public partial class PMMgrNavigationView : UserControl, IPartImportsSatisfiedNotification
    {
        private static Uri roleViewUri = new Uri(typeof(RoleView).FullName, UriKind.Relative);
        private static Uri staffRoleViewUri = new Uri(typeof(StaffRoleView).FullName, UriKind.Relative);
        private static Uri dictionaryViewUri = new Uri(typeof(DictionaryView).FullName, UriKind.Relative);
        private static Uri engineerViewUri = new Uri(typeof(EngineerView).FullName, UriKind.Relative);
        private static Uri consOrgViewUri = new Uri(typeof(ConsOrgView).FullName, UriKind.Relative);

        [Import]
        public IRegionManager regionManager;

        public PMMgrNavigationView()
        {
            // 为初始化变量所必需
            InitializeComponent();
            this.Loaded += (s, e) => { UpdateMainRegion(); };
        }

        void IPartImportsSatisfiedNotification.OnImportsSatisfied()
        {
            IRegion mainContentRegion = this.regionManager.Regions[RegionNames.MainContentRegion];
            if (mainContentRegion != null && mainContentRegion.NavigationService != null)
            {
                mainContentRegion.NavigationService.Navigated += this.MainContentRegion_Navigated;
            }
        }
        public void MainContentRegion_Navigated(object sender, RegionNavigationEventArgs e)
        {
            this.UpdateNavigationButtonState(e.Uri);
        }

        private void UpdateNavigationButtonState(Uri uri)
        {
            if (uri == roleViewUri)
            {
                this.btn_role.IsChecked = true;
            }
            else if (uri == staffRoleViewUri)
            {
                this.btn_staff_role.IsChecked = true;
            }
            else if (uri == dictionaryViewUri)
            {
                this.btn_dictionary.IsChecked = true;
            }
            else if (uri == engineerViewUri)
            {
                this.btn_engineer.IsChecked = true;
            }
            else if (uri == consOrgViewUri)
            {
                this.btn_cons_org.IsChecked = true;
            }
        }

        private void UpdateMainRegion()
        {
            if (this.btn_role.IsChecked == true)
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, roleViewUri);
            }
            else if (this.btn_staff_role.IsChecked == true)
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, staffRoleViewUri);
            }
            else if (this.btn_dictionary.IsChecked == true)
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, dictionaryViewUri);
            }
            else if (this.btn_engineer.IsChecked == true)
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, engineerViewUri);
            }
            else if (this.btn_cons_org.IsChecked == true)
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, consOrgViewUri);
            }
            else
            {
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, roleViewUri);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UpdateMainRegion();
        }
    }
}